package top.vanchor.flexible.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import top.vanchor.flexible.common.Res;
import top.vanchor.flexible.entity.Artist;
import top.vanchor.flexible.entity.Special;
import top.vanchor.flexible.entity.dto.ArtistListDto;
import top.vanchor.flexible.entity.dto.SpecialListDto;
import top.vanchor.flexible.service.IArtistService;
import top.vanchor.flexible.service.ISpecialService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/admin/artist")
public class ArtistController {
    @Resource
    private IArtistService service;

    /**
     * @param dto 对象
     * @return 列表
     */
    @ApiOperation("获取列表")
    @ResponseBody
    @PostMapping("/getList")
    public Res<IPage<Artist>> getList(@RequestBody ArtistListDto dto) {
        return Res.success(service.getList(dto));
    }

    @ApiOperation("添加 OR 编辑")
    @ResponseBody
    @PostMapping("/doEdit")
    public Res<IPage<Artist>> doEdit(@RequestBody Artist artist) {
        Integer id = artist.getId();
        int res = service.doEdit(artist);
        if (res > 0){
            return Res.success(id == null?"新增成功":"编辑成功");
        }
        return Res.error();
    }

    @ApiOperation("删除")
    @ResponseBody
    @PostMapping("/doDelete")
    public Res<JSONObject> doDelete(@RequestBody Map<String, List<Integer>> data) {
        //处理前端数据
        List<Integer> ids = data.get("ids");
        //初始化
        int delNum = 0;
        //执行
        delNum += service.doDeleteBy(ids);
        if (delNum >= ids.size()) {
            return Res.success("已删除" + delNum + "项");
        } else if (delNum > 0) {
            return Res.success("部分删除成功，" + delNum + "/" + ids.size());
        } else {
            return Res.error();
        }
    }
}
